package com.dmooo.ymx.activity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dmooo.ymx.MainActivity;
import com.dmooo.ymx.R;
import com.dmooo.ymx.base.BaseActivity;
import com.dmooo.ymx.common.SPUtils;
import com.dmooo.ymx.common.T;
import com.dmooo.ymx.config.Constants;
import com.zhouwei.mzbanner.MZBannerView;
import com.zhouwei.mzbanner.holder.MZHolderCreator;
import com.zhouwei.mzbanner.holder.MZViewHolder;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AdActivity extends BaseActivity {
    @BindView(R.id.img)
    ImageView img;
    @BindView(R.id.txt_time)
    TextView txtTime;
    @BindView(R.id.banner_image)
    MZBannerView banner;
    //    @BindView(R.id.last)
//    ImageView last;
//    @BindView(R.id.next)
//    ImageView next;
        int time = 5;
    int cur = 0;
    JSONArray array;
    private List<String> stringList = new ArrayList<>();

    @Override
    protected void initUI() {
        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
        setContentView(R.layout.activity_app_ad);
        ButterKnife.bind(this);
//        last.setVisibility(View.GONE);
//        next.setVisibility(View.GONE);
        banner.setBannerPageClickListener(new MZBannerView.BannerPageClickListener() {
            @Override
            public void onPageClick(View view, int i) {
                cur = i;
                try {
                    String type = array.getJSONObject(cur).getString("type");
                    String url = array.getJSONObject(cur).getString("href");
                    String value = array.getJSONObject(cur).getString("type_value");
                    if ("2".equals(type)) {
                        Intent intent = getPackageManager().getLaunchIntentForPackage("com.taobao.taobao");
                        if (intent != null) {
                            //获取剪贴板管理器：
                            ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                            // 创建普通字符型ClipData
                            ClipData mClipData = ClipData.newPlainText("Label", value);
                            // 将ClipData内容放到系统剪贴板里。
                            cm.setPrimaryClip(mClipData);
                            startActivity(intent);
                            return;
                        } else {
                            T.showShort(AdActivity.this, "未安装淘宝客户端");
                        }
                    } else if ("6".equals(type)) {
                        Intent intent = new Intent(AdActivity.this, WebViewActivity2.class);
                        intent.putExtra("title", "年货节");
                        intent.putExtra("url", "");
                        startActivity(intent);
                        return;
                    } else if ("7".equals(type) || "8".equals(type)) {
                    } else if ("10".equals(type)) {
                    }
                    if ("".equals(url)) {
                        return;
                    }
                    if (url != null) {
                        Intent intent = new Intent(AdActivity.this, WebViewActivity.class);
                        intent.putExtra("title", "详情");
                        intent.putExtra("url", url);
                        startActivity(intent);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
        banner.addPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                cur = position;
                if (position == stringList.size() - 1) {
//                    findViewById(R.id.txt_detail).setVisibility(View.VISIBLE);
                } else {
//                    findViewById(R.id.txt_detail).setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        try {
            JSONObject object = new JSONObject(SPUtils.getStringData(this, "ade", ""));
            if (object.getInt("code") == 0) {
                array = object.getJSONObject("data").getJSONArray("list");
                for (int i = 0; i < array.length(); i++) {
                    stringList.add(Constants.APP_IP + array.getJSONObject(i).getString("img"));
                }
                banner.setPages(stringList, new MZHolderCreator<BannerViewHolder>() {
                    @Override
                    public BannerViewHolder createViewHolder() {
                        return new BannerViewHolder();
                    }
                });
                banner.start();
//                if (array.length() > 1) {
//                    next.setVisibility(View.VISIBLE);
//                }
//                Glide.with(this).load(Constants.APP_IP + array.getJSONObject(0).getString("img")).into(img);
            }
        } catch (JSONException e) {
            Intent intent = new Intent(AdActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
        }
//        next.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                cur++;
//                if (cur == array.length() - 1) {
//                    next.setVisibility(View.GONE);
//                }
//                if (cur >= 1) {
//                    last.setVisibility(View.VISIBLE);
//                }
//                try {
//                    Glide.with(AdActivity.this).load(Constants.APP_IP + array.getJSONObject(cur).getString("img")).into(img);
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }
//            }
//        });
//        last.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                cur--;
//                if (cur == 0) {
//                    last.setVisibility(View.GONE);
//                }
//                next.setVisibility(View.VISIBLE);
//                try {
//                    Glide.with(AdActivity.this).load(Constants.APP_IP + array.getJSONObject(cur).getString("img")).into(img);
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }
//            }
//        });
    }

    @Override
    protected void initData() {
        txtTime.setText(time + "s");
        smokeHandle.sendEmptyMessageDelayed(0, 1000);
    }

    class BannerViewHolder implements MZViewHolder<String> {
        private ImageView mImageView;

        @Override
        public View createView(Context context) {
            View view = LayoutInflater.from(context).inflate(R.layout.img_t, null);
            mImageView = view.findViewById(R.id.img);
            return view;
        }

        @Override
        public void onBind(Context context, int position, final String data) {
            Glide.with(context).load(data).into(mImageView);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        flag=false;
    }

        boolean isClickAd=false;
    private boolean flag = true;
    private Handler smokeHandle = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            time--;
            if (time == 0&&flag&&!isClickAd) {
                txtTime.setText(time + "s");
                Intent intent = new Intent(AdActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
            if (flag) {
                txtTime.setText(time + "s");
                smokeHandle.sendEmptyMessageDelayed(0, 1000);
            }
        }
    };
    @Override
    protected void initListener() {

    }

    @Override
    protected void onResume() {
        super.onResume();
//        if(isClickAd){
//            Intent intent = new Intent(this, MainActivity.class);
//            startActivity(intent);
//            finish();
//        }
    }

    @OnClick({R.id.txt_detail, R.id.ll_skip})
    public void onViewClicked(View view) {
        Intent intent = new Intent(this, MainActivity.class);
        switch (view.getId()) {
            case R.id.txt_detail:
                isClickAd=true;
                startActivity(intent);
                finish();
                break;
            case R.id.ll_skip:
                isClickAd=true;
                startActivity(intent);
                finish();
                break;
        }
    }
}
